package com.woke.diyview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.AppCompatTextView;
import android.text.InputType;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * 密码控件
 */
public class PasswordView extends AppCompatTextView {

    private int mWidth;
    private int mHeight;
    private Paint mBorderPaint;//边框
    private Paint mPasswordPaint;//密码
    private int passwordLenth;//输入密码的长度
    private static final int PASSWORD_LENTH = 6;
    private static final int PASSWORD_RADIUS = 15;
    private TextWatcher mTextWatcher;


    public PasswordView(Context context) {
        super(context);
        init(null, 0);
    }

    public PasswordView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public PasswordView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD);
        setBackgroundColor(Color.WHITE);
        setTextColor(Color.TRANSPARENT);
        setCursorVisible(false);

        mBorderPaint = new Paint();
        mBorderPaint.setStrokeWidth(4);
        mBorderPaint.setColor(Color.parseColor("#838b8b"));

        mPasswordPaint = new Paint();
        mPasswordPaint.setColor(Color.BLACK);
        mPasswordPaint.setStrokeWidth(12);
    }

    public interface TextWatcher {
        void afterTextChanged(CharSequence charSequence);
    }

    public void setOnTextChangedListener(TextWatcher watcher) {
        mTextWatcher = watcher;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(widthSize, widthSize / PASSWORD_LENTH);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mWidth = getMeasuredWidth();
        mHeight = mWidth / PASSWORD_LENTH;

        //画四边
        mBorderPaint.setStrokeWidth(4);
        canvas.drawLine(0, 0, mWidth, 0, mBorderPaint);
        canvas.drawLine(0, mHeight, mWidth, mHeight, mBorderPaint);
        canvas.drawLine(0, 0, 0, mHeight, mBorderPaint);
        canvas.drawLine(mWidth, 0, mWidth, mHeight, mBorderPaint);

        //画框框内的分割线
        mBorderPaint.setStrokeWidth(2);
        for (int i = 1; i < (PASSWORD_LENTH); i++) {

            int offset = mWidth * i / PASSWORD_LENTH;
            canvas.drawLine(offset, 0, offset, mHeight, mBorderPaint);
        }
        mBorderPaint.setStrokeWidth(4);

        //密码用点代替
        int half = mWidth / PASSWORD_LENTH / 2;
        for (int i = 0; i < passwordLenth; i++) {
            int offset = mWidth * i / PASSWORD_LENTH + half;
            canvas.drawCircle(offset, half, PASSWORD_RADIUS, mPasswordPaint);
        }
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);

        if (TextUtils.isEmpty(text)) {
            passwordLenth = 0;
        } else {
            passwordLenth = text.length();
        }
        if (passwordLenth > 5) {
            passwordLenth = 6;
            if (mTextWatcher != null) {
                mTextWatcher.afterTextChanged(text.subSequence(0, 6));
            }
        }
        invalidate();
    }
}
